package com.bocai.kelalacar.ui.car.main.act;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.base.BaseRecyclerAdapter;
import com.bocai.kelalacar.bean.MainListBean;
import com.bocai.kelalacar.bean.PhoneTypeBean;
import com.bocai.kelalacar.bean.UserBean;
import com.bocai.kelalacar.ui.car.main.ada.MainAdapter;
import com.bocai.kelalacar.ui.car.main.contract.MainContract;
import com.bocai.kelalacar.ui.car.main.module.MainModule;
import com.bocai.kelalacar.ui.car.main.presenter.MainPresenter;
import com.bocai.kelalacar.ui.car.set.act.CertificationInformationStateActivity;
import com.bocai.kelalacar.ui.car.set.act.ServiceMarginActivity;
import com.bocai.kelalacar.ui.car.set.act.SetActivity;
import com.bocai.kelalacar.ui.driver.main.act.DriverPublishInforAddressActivity;
import com.bocai.kelalacar.ui.driver.set.act.DriverCertificationInformationStateActivity;
import com.bocai.kelalacar.util.SP;
import com.bocai.kelalacar.util.ToastUitl;
import com.bocai.kelalacar.widget.LoginDialog;
import com.bocai.kelalacar.widget.TeamUserPopupWindow;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.tencent.bugly.crashreport.CrashReport;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * Created by yuanfei on 2017/6/19.
 */

public class MainActivity extends BaseActivity implements MainContract.view, View.OnClickListener, XRecyclerView.LoadingListener {
    @BindView(R.id.personal)
    LinearLayout personal;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.btn_select)
    Button btnSelect;
    @BindView(R.id.xrecycle)
    XRecyclerView xrecycle;
    @BindView(R.id.pub_img)
    ImageView pubImg;
    @BindView(R.id.ll_pub)
    LinearLayout llPub;
    MainAdapter mainAdapter;
    //    TeamUserPopupWindow userPopupWindow;
    boolean isShow = true;
    Animation outAnimation;
    Animation inAnimation;
    LoginDialog loginDialog;
    @Inject
    MainPresenter presenter;

    private int index = 0;
    private int pageIndex = 1;
    private int login = 0;

    @Override
    public int getLayoutId() {
        return R.layout.main;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new MainModule(this)).inject(this);
    }

    @Override
    public void initView() {
//        if ((int)SP.get(mContext,"MemberType",0) == 3){
        initToolbar("客拉拉司机端");
//        }else {
//            initToolbar("客拉拉车队端");
//        }

        loginDialog = new LoginDialog(this).builder().setTitle("登录").setConfirmButton("下一步");
        loginDialog.setPresenter(presenter);
        xrecycle.setLayoutManager(new LinearLayoutManager(this));
        mainAdapter = new MainAdapter();
        xrecycle.setAdapter(mainAdapter);
        outAnimation = AnimationUtils.loadAnimation(this, R.anim.fab_out);
        inAnimation = AnimationUtils.loadAnimation(this, R.anim.fab_in);

        xrecycle.setRefreshProgressStyle(ProgressStyle.BallBeat);
        xrecycle.setLoadingListener(this);
//        xrecycle.refresh();

//        userPopupWindow = new TeamUserPopupWindow(this,presenter);
//        userPopupWindow.setData();
        personal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(UserActivity.class);
//                overridePendingTransition(R.anim.user_open,0);
                if (!SP.get(mContext, "id", "").toString().equals("")) {
//                    userPopupWindow.showAsDropDown(findViewById(R.id.top_line),  0, 0);
//                    userPopupWindow.getUser();
                    startActivity(UserActivity.class);
                    overridePendingTransition(R.anim.user_open, R.anim.without);
                } else {
                    setDialog();
                }
//                presenter.login("13112345678","111111");

            }
        });
        btnSelect.setOnClickListener(this);
        pubImg.setOnClickListener(this);

        xrecycle.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                Log.e("dy", dy + "");
                if (dy > 5 && isShow) {
                    isShow = false;
                    setAnimOut();
                }
                if (dy < -5 && !isShow) {
                    isShow = true;
                    setAnimIn();
                }
            }
        });

        llPub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("setOnClickListener", "setOnClickListener");
            }
        });

        mainAdapter.setOnItemOnListener(new BaseRecyclerAdapter.OnItemClickedListener() {
            @Override
            public void onItemClicked(int id, int index) {
                if (TextUtils.isEmpty(SP.get(MainActivity.this, "id", "").toString())) {
                    setDialog();
//                    showErrorTip("请先登录");
                } else {
                    startActivity(new Intent(MainActivity.this, TravelDetailActivity.class).putExtra("orderId", mainAdapter.getDataList().get(index).getOrderId()));
                }
//                startActivity(TravelDetailActivity.class);
            }
        });
        llRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(SP.get(MainActivity.this, "id", "").toString())) {
                    setDialog();
//                    showErrorTip("请先登录");
                } else {
                    startActivity(MessageActivity.class);
                }
            }
        });
    }

    @Override
    public void showLoading(String title) {

        startProgressDialog(title);

    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")) {
//            userPopupWindow.dismiss();
            setDialog();
        } else if (msg.equals("网络异常")) {
            xrecycle.refreshComplete();
            xrecycle.loadMoreComplete();
        } else if (msg.equals("非法手机号")) {
            loginDialog.setEnterPhone();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_select:
                if (TextUtils.isEmpty(SP.get(MainActivity.this, "id", "").toString())) {
                    setDialog();
//                    showErrorTip("请先登录");
                } else {
                    startActivity(SelectActivity.class);
                }
                break;
            case R.id.pub_img:
                if (TextUtils.isEmpty(SP.get(MainActivity.this, "id", "").toString())) {
                    setDialog();
//                    showErrorTip("请先登录");
                }  else if ((int) SP.get(this, "ApprovalStatus", 0) == 0) {
                    if ((int) SP.get(this, "MemberType", 0) == 3) {
                        startActivity(new Intent(this, DriverCertificationInformationStateActivity.class));
                    } else {
                        startActivity(new Intent(this, CertificationInformationStateActivity.class));
                    }
                } else if ((int) SP.get(this, "ApprovalStatus", 0) == -1) {
                    if ((int) SP.get(this, "MemberType", 0) == 3) {
                        startActivity(new Intent(this, DriverCertificationInformationStateActivity.class));
                    } else {
                        startActivity(new Intent(this, CertificationInformationStateActivity.class));
                    }
                } else if ((int) SP.get(this, "ApprovalStatus", 0) == 1) {
                    login = 1;
                    presenter.getUser();
//                    ToastUitl.showShort("审核中");
                }else if (!(boolean) SP.get(MainActivity.this, "PayBail", false)) {
                    startActivity(ServiceMarginActivity.class);
                } else {
                    if ((int) SP.get(mContext, "MemberType", 0) == 3) {
                        startActivity(DriverPublishInforAddressActivity.class);
                    } else {
                        startActivity(PublishInformationAddressActivity.class);
                    }
                }

                break;
        }
    }

//    public void setAnimOut() {
//        ValueAnimator animator = ValueAnimator.ofFloat(0,llPub.getTop());
//        animator.setTarget(llPub);
//        animator.setDuration(500).start();
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
//        {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation)
//            {
//                Log.e("onAnimationUpdate",animation.getAnimatedValue()+"");
//                llPub.setTranslationY((Float) animation.getAnimatedValue());
//            }
//        });
//    }
//    public void setAnimIn() {
//        ValueAnimator animator = ValueAnimator.ofFloat(llPub.getTop(),0);
//        animator.setTarget(llPub);
//        animator.setDuration(500).start();
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
//        {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation)
//            {
//                Log.e("onAnimationUpdate",animation.getAnimatedValue()+"");
//                llPub.setTranslationY((Float) animation.getAnimatedValue());
//            }
//        });
//    }

    public void setAnimOut() {
        ValueAnimator animator = ValueAnimator.ofFloat(0, llPub.getTop());
        animator.setTarget(llPub);
        animator.setDuration(500).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.e("onAnimationUpdate", animation.getAnimatedValue() + "");
                llPub.setTranslationY((Float) animation.getAnimatedValue());
            }
        });
    }

    public void setAnimIn() {
        ValueAnimator animator = ValueAnimator.ofFloat(llPub.getTop(), 0);
        animator.setTarget(llPub);
        animator.setDuration(500).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Log.e("onAnimationUpdate", animation.getAnimatedValue() + "");
                llPub.setTranslationY((Float) animation.getAnimatedValue());
            }
        });
    }

    public void setDialog() {
        login = 0;
        loginDialog.setEnterPhone();
        loginDialog.show();
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.e("onDismiss", "onDismiss111111");
    }

    @Override
    public void getCode(String code) {
        presenter.setCode();
        loginDialog.setSmsCode(code);
    }

    @Override
    public void getType(PhoneTypeBean typeBean) {
        if (typeBean.isRegisterPhone() && typeBean.isSetPassword()) {
            if (typeBean.getMemberType().equals("2")) {
                loginDialog.setLogin(2);
            } else if (typeBean.getMemberType().equals("3")) {
                loginDialog.setLogin(3);
            } else {
                loginDialog.setEnterPhone();
                showErrorTip("此账号为旅行社端账号");
            }

        } else {
            if (typeBean.getMemberType().equals("2")) {
                loginDialog.setPhotoCode(2);
            } else if (typeBean.getMemberType().equals("3")) {
                loginDialog.setPhotoCode(3);
            } else if (typeBean.getMemberType().equals("0")) {
                loginDialog.setBtnSelect();
            } else {
                loginDialog.setEnterPhone();
                showErrorTip("此账号为旅行社端账号");
            }
        }
    }

    @Override
    public void getUser(UserBean userBean) {
        if (userBean == null || userBean.getId() == null) {
            loginDialog.setBtn();
        } else {
            SP.putUser(MainActivity.this, userBean);
//            userPopupWindow.setData();
            loginDialog.dismiss();
            if ((int) SP.get(mContext, "MemberType", 0) == 3) {
                initToolbar("客拉拉司机端");
            } else {
                initToolbar("客拉拉司机端");
            }

            if (login == 1){
                if ((int) SP.get(this, "ApprovalStatus", 0) == 0) {
                    if ((int) SP.get(this, "MemberType", 0) == 3) {
                        startActivity(new Intent(this, DriverCertificationInformationStateActivity.class));
                    } else {
                        startActivity(new Intent(this, CertificationInformationStateActivity.class));
                    }
                } else if ((int) SP.get(this, "ApprovalStatus", 0) == -1) {
                    if ((int) SP.get(this, "MemberType", 0) == 3) {
                        startActivity(new Intent(this, DriverCertificationInformationStateActivity.class));
                    } else {
                        startActivity(new Intent(this, CertificationInformationStateActivity.class));
                    }
                } else if ((int) SP.get(this, "ApprovalStatus", 0) == 1) {
//                    presenter.getUser();
                    ToastUitl.showShort("审核中");
                }else if (!(boolean) SP.get(MainActivity.this, "PayBail", false)) {
                    startActivity(ServiceMarginActivity.class);
                } else {
                    if ((int) SP.get(mContext, "MemberType", 0) == 3) {
                        startActivity(DriverPublishInforAddressActivity.class);
                    } else {
                        startActivity(PublishInformationAddressActivity.class);
                    }
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 3) {
//            userPopupWindow.setData();
        }
//        xrecycle.refresh();
    }

    @Override
    public void setBtnCode(int index) {
        loginDialog.setBtnCode(index);
    }

    @Override
    public void getHomeListSuccess(MainListBean bean) {
        xrecycle.refreshComplete();
        xrecycle.loadMoreComplete();
        tvNum.setText("当前有" + 0 + "个任务");
        if (bean != null && bean.getOrderList() != null) {
            if (pageIndex == 1) {
                tvNum.setText("当前有" + bean.getTotalCount() + "个任务");
                mainAdapter.clear();
                mainAdapter.addItems(bean.getOrderList());
            } else {
                mainAdapter.addItems(bean.getOrderList());
            }
            if (bean.getOrderList().size() > 10) {
                xrecycle.setLoadingMoreEnabled(false);
            } else {
                xrecycle.setLoadingMoreEnabled(true);
            }
        } else {

        }
    }

    @Override
    public void onRefresh() {
        pageIndex = 1;
        presenter.homeList(pageIndex + "", "10", "", "0", "", "");
    }

    @Override
    public void onLoadMore() {
        pageIndex = pageIndex + 1;
        presenter.homeList(pageIndex + "", "10", "", "0", "", "");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (intent != null) {
            index = getIntent().getIntExtra("index", 0);
            if (index == 1) {
                SP.put(this, "id", "");
                setDialog();
//                userPopupWindow.dismiss();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        xrecycle.refresh();
    }
}
